package cn.itcast.mybatis.dao;

import cn.itcast.mybatis.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/3.
 */
public class UserMapperTest2 {
    public UserMapper userMapper;
    SqlSession sqlSession;
    @Before
    public void setUp() throws Exception {
        // 读取mybatis的全局配置文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 构建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 获取sqlSession会话
        sqlSession = sqlSessionFactory.openSession();

        // 初始化userDao
        this.userMapper = sqlSession.getMapper(UserMapper.class);
    }
    @Test
    public void testQueryUserListByIds(){
        List<User> users = this.userMapper.queryUserListByIds(new Long[]{1l,2l,3l,4l});
        for (User user : users) {
            System.out.println(user);
        }
    }



    @Test
    public void testUpdateUserSelective(){
        User user = new User();
        user.setAge(38);
        user.setName("波多野结衣");
        user.setPassword("123456");
        user.setUserName("boduo");
//		user.setSex(3);
        user.setBirthday(new Date());
        user.setId(7l);
        this.userMapper.updateUserSelective(user);
    }


    @Test
    public void testQueryUserListLikeUserNameAndAge(){
        System.out.println(this.userMapper.queryUserListLikeUserNameAndAge("li",23));
    }

    @Test
    public void testQueryUserListLikeUserNameOrAge(){
        System.out.println(this.userMapper.queryUserListLikeUserNameOrAge("",16));
    }

    @Test
    public void testQueryUserListLikeUserName(){
        System.out.println(this.userMapper.queryUserListLikeUserName(""));
    }

    @Test
    public void testQueryUserById() {
        System.out.println(this.userMapper.queryUserById(1l));
    }

    @Test
    public void testQueryUserAll() {
        List<User> userList = this.userMapper.queryUserAll();
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testInsertUser() {
        User user = new User();
        user.setAge(16);
        user.setBirthday(new Date());
        user.setName("小老鼠");
        user.setPassword("123456");
        user.setSex(2);
        user.setUserName("xiaolaoshu");
        this.userMapper.insertUser(user);
    }

    @Test
    public void testUpdateUser() {
        User user = userMapper.queryUserById(7l);
        user.setName("秋水");
        user.setUserName("qiushui");
        user.setSex(1);
        this.userMapper.updateUser(user);
    }

    @Test
    public void testDeleteUser() {
        this.userMapper.deleteUser(8l);
    }

    @Test
    public void testLoginUser(){
       User user = this.userMapper.loginUser("qiu");
        System.out.println(user);
    }
    @After
    public void after(){
        sqlSession.commit();
    }
}
